package tsinghua.smxq.mc;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import android.widget.ZoomControls;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKBusLineResult;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKPlanNode;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MKSearchListener;
import com.baidu.mapapi.MKTransitRouteResult;
import com.baidu.mapapi.MKWalkingRouteResult;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.RouteOverlay;
import com.baidu.mapapi.TransitOverlay;

public class BusRoutePlanActivity extends MapActivity {

	MapView mMapView = null;
	MKSearch mSearch = null;
	ImageButton mBtnTransit = null;
	ImageButton mBtnChooseType = null;
	Button backToMain = null;

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.busrouteplan);

		BaiduMap app = (BaiduMap) this.getApplication();
		if (app.bMapMan == null) {
			app.bMapMan = new BMapManager(getApplication());
			app.bMapMan.init(app.myKey, new BaiduMap.MyGeneralListener());
		}
		app.bMapMan.start();
		super.initMapActivity(app.bMapMan);

		mMapView = (MapView) findViewById(R.id.bmapView);
		mMapView.setBuiltInZoomControls(true);
		mMapView.setDrawOverlayWhenZooming(true);
		ZoomControls zoomControls = (ZoomControls) mMapView.getChildAt(2);
		zoomControls.setPadding(0, 0, 20, 170);
		mSearch = new MKSearch();
		mSearch.init(app.bMapMan, new MKSearchListener() {

			public void onGetDrivingRouteResult(MKDrivingRouteResult res,
					int error) {
				if (error != 0 || res == null) {
					Toast.makeText(BusRoutePlanActivity.this, "抱歉，未找到结果",
							Toast.LENGTH_SHORT).show();
					return;
				}
				RouteOverlay routeOverlay = new RouteOverlay(
						BusRoutePlanActivity.this, mMapView);
				routeOverlay.setData(res.getPlan(0).getRoute(0));
				mMapView.getOverlays().clear();
				mMapView.getOverlays().add(routeOverlay);
				mMapView.invalidate();

				mMapView.getController().animateTo(res.getStart().pt);
			}

			public void onGetTransitRouteResult(MKTransitRouteResult res,
					int error) {
				if (error != 0 || res == null) {
					Toast.makeText(BusRoutePlanActivity.this, "抱歉，未找到结果",
							Toast.LENGTH_SHORT).show();
					return;
				}
				TransitOverlay routeOverlay = new TransitOverlay(
						BusRoutePlanActivity.this, mMapView);
				routeOverlay.setData(res.getPlan(0));
				mMapView.getOverlays().clear();
				mMapView.getOverlays().add(routeOverlay);
				mMapView.invalidate();

				mMapView.getController().animateTo(res.getStart().pt);
			}

			public void onGetWalkingRouteResult(MKWalkingRouteResult res,
					int error) {
				if (error != 0 || res == null) {
					Toast.makeText(BusRoutePlanActivity.this, "抱歉，未找到结果",
							Toast.LENGTH_SHORT).show();
					return;
				}
				RouteOverlay routeOverlay = new RouteOverlay(
						BusRoutePlanActivity.this, mMapView);
				routeOverlay.setData(res.getPlan(0).getRoute(0));
				mMapView.getOverlays().clear();
				mMapView.getOverlays().add(routeOverlay);
				mMapView.invalidate();

				mMapView.getController().animateTo(res.getStart().pt);
			}

			public void onGetAddrResult(MKAddrInfo res, int error) {
			}

			public void onGetPoiResult(MKPoiResult res, int arg1, int arg2) {
			}

			public void onGetBusDetailResult(MKBusLineResult result, int iError) {
			}
		});

		mBtnTransit = (ImageButton) findViewById(R.id.transit);

		mBtnTransit.setOnClickListener(new ImageButton.OnClickListener() {
			public void onClick(View v) {
				SearchButtonProcess(v);
			}

		});
		
		backToMain = (Button) findViewById(R.id.buslinesearch_back);
		backToMain.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				BusRoutePlanActivity.this.startActivity(new Intent(
						BusRoutePlanActivity.this, MobileCityActivity.class));
				BusRoutePlanActivity.this.overridePendingTransition(
						R.anim.push_in, R.anim.push_out);
				BusRoutePlanActivity.this.finish();
			}

		});
		mBtnChooseType = (ImageButton) findViewById(R.id.ib_chooseType);
		mBtnChooseType.setOnClickListener(new ImageButton.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new AlertDialog.Builder(BusRoutePlanActivity.this)
						.setTitle("选择搜索类型")
						.setItems(new String[] { "公交线路搜索", "公交路线规划" },
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										// TODO Auto-generated method stub
										if (which == 0) {
											Intent intent = new Intent();
											intent.setClass(
													BusRoutePlanActivity.this,
													BusLineSearchActivity.class);
											BusRoutePlanActivity.this
													.startActivity(intent);
										} else if (which == 1) {

										}

									}
								}).create().show();
			}

		});

	}

	void SearchButtonProcess(View v) {
		EditText editSt = (EditText) findViewById(R.id.start);
		EditText editEn = (EditText) findViewById(R.id.end);

		MKPlanNode stNode = new MKPlanNode();
		stNode.name = editSt.getText().toString();
		MKPlanNode enNode = new MKPlanNode();
		enNode.name = editEn.getText().toString();

		mSearch.transitSearch("北京", stNode, enNode);
	}

	@Override
	protected void onPause() {
		BaiduMap app = (BaiduMap) this.getApplication();
		app.bMapMan.stop();
		super.onPause();
	}

	@Override
	protected void onResume() {
		BaiduMap app = (BaiduMap) this.getApplication();
		app.bMapMan.start();
		super.onResume();
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

}
